package cbfg.http.demo.gank

import android.os.Bundle
import android.text.method.ScrollingMovementMethod
import android.view.View
import android.widget.AdapterView
import androidx.fragment.app.Fragment
import cbfg.http.HttpManager
import cbfg.http.demo.GsonHolder
import cbfg.http.demo.R
import cbfg.http.demo.gank.entity.BaseGankEntity
import cbfg.http.demo.gank.entity.BaseGankList
import cbfg.http.demo.gank.entity.GankItem
import cbfg.http.demo.gank.entity.SubCategory
import cbfg.http.subscribe.Disposable
import kotlinx.android.synthetic.main.fragment_gank.*

/**
 * @author:  Tom Hawk
 * 添加时间: 2020/9/17 14:18
 * 功能描述:
 */
class GankFragment : Fragment(R.layout.fragment_gank), AdapterView.OnItemSelectedListener {
    companion object {
        private const val PAGE_LIMIT = "5"
    }

    private lateinit var adapter: SubCategoryAdapter

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        tvResult.movementMethod = ScrollingMovementMethod.getInstance()
        spCategories.onItemSelectedListener = this
        spPages.onItemSelectedListener = this

        adapter = SubCategoryAdapter(view.context)
        spSubCategories.adapter = adapter
        spSubCategories.onItemSelectedListener = this

        refreshSubCategories(spCategories.getItemAtPosition(0).toString())
    }

    private fun getContentList() {
        if (adapter.isEmpty) {
            return
        }
        HttpManager.getService(GankService::class.java)
            .getContentList(
                spCategories.selectedItem.toString(),
                adapter.getItem(spSubCategories.selectedItemPosition).type,
                spPages.selectedItem.toString(),
                PAGE_LIMIT
            )
            //.subscribeOn(ThreadMode.ASYNC) //默认 ThreadMode.ASYNC
            //.observeOn(ThreadMode.MAIN) //默认 ThreadMode.MAIN
            .subscribe(object : GankObserver<BaseGankList<GankItem>>(lifecycle) {
                override fun onSubscribe(disposable: Disposable) {
                    super.onSubscribe(disposable)
                    tvResult.text = null
                }

                override fun onSuccess(result: BaseGankList<GankItem>) {
                    tvResult.text = GsonHolder.gson.toJson(result)
                }

                override fun onError(e: Throwable) {
                    tvResult.text = e.message
                }
            })
    }

    private fun getContentList2() {
        if (adapter.isEmpty) {
            return
        }
        HttpManager.getService(GankService::class.java)
            .getContentList2(
                spCategories.selectedItem.toString(),
                adapter.getItem(spSubCategories.selectedItemPosition).type,
                spPages.selectedItem.toString(),
                PAGE_LIMIT
            )
            .subscribe(object : GankObserver<String>(lifecycle) {
                override fun onSubscribe(disposable: Disposable) {
                    super.onSubscribe(disposable)
                    tvResult.text = null
                }

                override fun onSuccess(result: String) {
                    tvResult.text = result
                }

                override fun onError(e: Throwable) {
                    tvResult.text = e.message
                }
            })
    }

    private fun refreshSubCategories(parent: String) {
        HttpManager.getService(GankService::class.java)
            .getSubCategories(parent)
            .subscribe(object : GankObserver<BaseGankEntity<List<SubCategory>>>(lifecycle) {
                override fun onSubscribe(disposable: Disposable) {
                    super.onSubscribe(disposable)
                    adapter.setItems(null)
                }

                override fun onSuccess(result: BaseGankEntity<List<SubCategory>>) {
                    adapter.setItems(result.data)
                }

                override fun onError(e: Throwable) {
                    tvResult.text = e.message
                }
            })
    }

    override fun onNothingSelected(parent: AdapterView<*>?) {}
    override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {
        when (parent.id) {
            R.id.spCategories -> refreshSubCategories(parent.getItemAtPosition(position).toString())
            R.id.spSubCategories -> {
                //getContentList()
                getContentList2()
            }
            R.id.spPages -> {
                //getContentList()
                getContentList2()
            }
        }
    }
}